package com.spzx.order.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.common.rabbit.service.RabbitService;
import com.spzx.order.service.IOrderInfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class OrderReceiver {

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private RabbitService rabbitService;


    /**
     * 处理延迟消息：关单 -> 发消息解锁库存
     * @param orderIdStr
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(queues = MqConst.QUEUE_CANCEL_ORDER)
    public void processCloseOrder(String orderIdStr, Message message, Channel channel) /*throws IOException*/ {
        if(StringUtils.isNotEmpty(orderIdStr)){
            Long orderId = Long.parseLong(orderIdStr);
            //关单
            orderInfoService.processCloseOrder(orderId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}